<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades" /> 权限配置
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="handAdd"
        >新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="menuName"
        border
        :tree-props="{children: 'menus', hasChildren: 'hasChildren'}"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="menuName" label="权限名称" />
        <el-table-column prop="menuPath" label="访问路径" />
        <el-table-column prop="menuPerms" label="权限" />
        <el-table-column prop="menuType" label="权限类型" :formatter="typeFormatter" />
        <el-table-column prop="menuDescribe" label="权限描述" />
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        />
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item v-show="form.menuPerms!='root'" label="父权限ID">
          <el-cascader
            v-model="form.menuRefParentid"
            :options="options"
            :props="{ checkStrictly: true ,value:'menuId',label:'menuName',children:'menus',emitPath:false,expandTrigger:'hover'}"
          />
          {{ form.menuRefParentid }}
        </el-form-item>
        <el-form-item label="权限类型">
          <el-radio-group v-model="form.menuType">
            <el-radio :label="0">菜单</el-radio>
            <el-radio :label="1">按钮</el-radio>
            <el-radio :label="2">接口</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限名称" prop="menuName">
          <el-input v-model="form.menuName" />
        </el-form-item>
        <el-form-item label="权限路径" prop="menuPath">
          <el-input v-model="form.menuPath" />
        </el-form-item>
        <el-form-item label="权限属性" prop="menuPerms">
          <el-input v-model="form.menuPerms" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.menuDescribe" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { saveMenu } from '@/api/menu'
import { getTree } from '@/api/menu'
import { delMenu } from '@/api/menu'
export default {
  name: 'BaseMenu',
  data() {
    return {
      query: {
        address: '',
        name: '',
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      options: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,

      rules: {
        menuName: [
          { required: true }
        ],
        menuPath: [
          { required: true }
        ],
        menuPerms: [
          { required: true }
        ],
        menuType: [
          { required: true }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      getTree(this.query).then(res => {
        this.tableData = res.data.tree
        this.pageTotal = res.data.tree.length || 50
      })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        delMenu(row.menuId).then(res => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
        .catch(() => {
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    delAllSelection() {
      const length = this.multipleSelection.length
      let str = ''
      this.delList = this.delList.concat(this.multipleSelection)
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' '
      }
      this.$message.error(`删除了${str}`)
      this.multipleSelection = []
    },
    // 保存编辑
    saveEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          saveMenu(this.form).then(res => {
            this.$message.success(`保存成功`)
            this.editVisible = false
          })
        } else {
          this.$message.success(`请填写必填项`)
        }
      }
      )
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index
      this.form = row
      this.editVisible = true
      getTree().then(res => {
        this.options = res.data.tree
      })
    },
    handAdd() {
      this.form = {}
      this.editVisible = true
      getTree().then(res => {
        this.options = res.data.tree
      })
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    },
    typeFormatter(cellValue) {
      if (cellValue.menuType === 0) {
        return '菜单'
      } else if (cellValue.menuType === 1) {
        return '按钮'
      } else {
        return '接口'
      }
    }
  }
}
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.el-col {
  margin-left: 40px;
  margin-right: 40px;
}

</style>
